package com.flyme.module.base.provider.controller;

import com.flyme.common.core.model.ResultBody;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysExaenumRemoteService;
import com.flyme.module.base.client.entity.SysExaenum;
import com.flyme.module.base.client.entity.Sysenum;
import com.flyme.module.base.provider.service.SysExaenumService;
import com.flyme.module.base.provider.service.SysExaminationService;
import com.flyme.module.base.provider.service.SysenumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统枚举分类（打分时使用）管理
 * @author zyh
 * @date 2020-8-5
 */
@Api(tags = "系统枚举分类（打分时使用）")
@RestController
public class SysExaenumController extends BaseController<SysExaenumService, SysExaenum> implements SysExaenumRemoteService {

    @Autowired
    private SysenumService enumService;
    @Autowired
    private SysExaminationService examinationService;

    @Override
    public ResultBody getEnumItem() {
        EntityMap map = new EntityMap();
        List<SysExaenum> list = bizService.list();
        List<SysExaenum> educationList = new ArrayList<SysExaenum>();
        List<SysExaenum> worksList = new ArrayList<SysExaenum>();
        for(SysExaenum e : list){
            if("xueli".equals(e.getEnumType())){
                educationList.add(e);
            }
            if("workyear".equals(e.getEnumType())){
                worksList.add(e);
            }
        }
        map.put("educationList", educationList);
        map.put("worksList", worksList);
        return ResultBody.success(map);
    }

    @Override
    public List<Map<String, Object>> listEnum(){
        List<Map<String, Object>> list = enumService.findByType("industry");
        return list;
    }

    @ApiOperation(value = "添加行业", notes = "添加行业")
    @Override
    public ResultBody addIndustry(@ModelAttribute Sysenum sysEnum) {
        Boolean tag = enumService.saveOrUpdate(sysEnum);
        return ResultBody.success("操作成功").setData(tag);
    }

    @Override
    public ResultBody delById(@PathVariable Long id) {
        Integer num = examinationService.countByEnum(id);
        if(num > 0){
            return ResultBody.failed("该行业下有考试安排");
        }
        Boolean tag = enumService.removeById(id);
        if(tag){
            return ResultBody.success("操作成功");
        }else{
            return ResultBody.failed("操作失败");
        }
    }
}
